<template>
    <div>
        <uc-table url="admin/maintenanceRecord/getInMaintenanceInfoPage" ref="maintenanceInfo" name="维保任务" table-border pageable :search-form="searchForm">
            <template slot="searchForm">
                <el-form size="mini" :inline="true" :model="searchForm" class="demo-form-inline filter-container">
                  <!--搜索输入框-->
                  <el-form-item>
                    <el-input type="text" v-model="searchForm.title" placeholder="任务标题" clearable @clear="clearInput()"></el-input>
                  </el-form-item>
                  <el-form-item v-if="enterPriseHidden">
                    <el-select v-model="searchForm.enterpriseId" placeholder="请选择主体"  clearable @clear="clearInput">
                      <el-option v-for="(item,index) in enterpriseDominantList" :key="index"  :value="item.ucId" :label="item.name"></el-option>
                    </el-select>
                  </el-form-item>
                    <el-form-item>
                      <el-button type="primary" icon="el-icon-search" class="filter-item" @click="handleSearch()">查询</el-button>
                      <el-button type="success"  icon="el-icon-plus" class="filter-item" @click="handleMaintenanceTask()">生成维保任务</el-button>
                    </el-form-item>
                </el-form>
            </template>
            <template slot="columns">
                <el-table-column prop="title" label="任务标题" align="left" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="maintenaceEnterpriseName" label="维保企业" align="left" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="enterpriseName" label="业主企业" align="left" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="userName" label="维保人员" align="left" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column prop="status" label="工单状态" align="left" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        {{scope.row.status | maintenanceTaskStatus}}
                    </template>
                </el-table-column>
                <el-table-column prop="address" label="操作" width="180" align="left" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
<!--                        <el-button  size="mini" icon="el-icon-edit" type="primary" @click="handleEdit(scope.row)" >编辑</el-button>-->
                        <el-button size="mini" icon="el-icon-info" type="success" @click="handleInfo(scope.row)">详情</el-button>
                        <el-button size="mini" type="danger" icon="el-icon-delete" :loading="deleteLoading" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </template>
        </uc-table>
    </div>
</template>

<script>
import UcTable from "@/components/UcTable/index.vue";
import {removeMaintenanceInfo} from "@/api/maintenanceRecord";
import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";
import {handleMaintenanceTask} from "@/api/maintenanceRecord";

export default {
    name: "list",
    components: {UcTable},
    data(){
        return{
            searchForm:{},
            deleteLoading:false,
            enterpriseDominantList:[]
        }
    },
    computed:{
      enterPriseHidden(){
        let roleCode = this.$store.getters.roleCode;
        return roleCode=="ADMIN"||roleCode=="XT-ADMIN"||roleCode=="ZD-ADMIN"||roleCode=="DD-ADMIN"||roleCode=="JW-ADMIN"||roleCode=="XZ-ADMIN"||roleCode=="WB-ADMIN"
      }
    },
    created() {
      this.getInEnterpriseDominantList()
    },
    methods: {
      clearInput(){
        //清除输入框
        this.searchForm.name = null
        this.handleSearch()
      },
        handleSearch() {
            this.$refs.maintenanceInfo.getTableData(1)
        },
        handleEdit(val) {
            this.$router.push("/maintenance/maintenanceTaskEdit/" + val.ucId)
        },
        handleInfo(val) {
            this.$router.push("/maintenance/maintenanceTaskInfo?ucId=" + val.ucId+"&qyId="+val.enterpriseId)
        },
        handleDelete(val) {
            this.$confirm('您确定要执行删除' + val.name + '这条维保任务吗？', '提醒信息', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.deleteLoading = true
                removeMaintenanceInfo(val).then(res => {
                    this.deleteLoading = false
                    this.$notify.success({
                        title: '成功',
                        message: "删除维保任务成功"
                    })
                    this.handleSearch()
                }).catch(res => {
                    this.deleteLoading = false
                    this.$notify.error({
                        title: '失败',
                        message: res.msg
                    })
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
      getInEnterpriseDominantList(){
        getInEnterpriseDominantList({deleted:false}).then(res=>{
          this.enterpriseDominantList = res.data
        }).catch(error=>{

        })
      },
      handleMaintenanceTask(){
        handleMaintenanceTask().then(res=>{
          this.$notify.success({
            title:'成功',
            message:'生成维保任务成功!'
          })
          this.handleSearch()
        }).catch(()=>{
          this.$notify.error({
            title:'失败',
            message:'生成维保任务失败!'
          })
        })
      }
    }
}
</script>

<style scoped>

</style>
